<?php

$weid=$_W['weid'];
$operation = $_GPC['op'];
$from=$_W['fans']['from_user'];
$result = array('status' => 0, 'message' => '');
$goodsid = intval($_GPC['dishes_id']);
$goods = pdo_fetch("SELECT * FROM ".tablename('shopping3_cart')." WHERE from_user ='".$from."' AND weid = '{$weid}' ");

 
if (empty($goods)) {
		$result['message'] = '抱歉，购物车中没有菜品！';
		message($result, '', 'ajax');
}
    $glist=unserialize($goods["goodsinfo"]);
    if(!$glist[$goodsid]){
        
		$result['message'] = '抱歉，购物车中没有此菜品！';
		message($result, '', 'ajax');        
    }
    $glist[$goodsid]=$_GPC['total'];
    if($_GPC['total']==0){
        if($goods["pakageid"]>0){
            $row = pdo_fetch("SELECT id,dishes FROM ".tablename('shopping3_genius')." WHERE id= ".$goods["pakageid"]);
            $genlist=unserialize($row["dishes"]);
            if(in_array($goodsid,$genlist["id"])){
                
		      $result['message'] = '不能取消套餐中的菜品！';
		      message($result, '', 'ajax');  
                        
                $glist[$goodsid]=1;
            }else{
               // unset($glist[$goodsid]);
            }
            
        }else{
           // unset($glist[$goodsid]);
        }        
    }
    $totalnum=0;
    $dishlist=array();
    while(list($k,$v)=each($glist)){
        if(!empty($v)){
        $totalnum=$totalnum+$v;
        $dishlist[]=$k;
        $dishalllist[$k]=$v;
        }
    }
    
    
    if($goods["pakageid"]>0){
         $info=pdo_fetch("select * from ".tablename('shopping3_genius')." where id=".$goods["pakageid"]);
         
        $list=unserialize($info["dishes"]);
        
        $gdishlist=$list["id"];
         
        $totalprice=$info["totalprice"];
    }else{
        $totalprice=0;
        
    }
         $clist=pdo_fetchall("select id,productprice from ".tablename('shopping3_goods')."  where id in (".implode(",",$dishlist).")");
         while(list($k,$v)=each($clist)){
             $cid=$v["id"];
             if(in_array($cid,$gdishlist)){
                if($dishalllist[$cid]>1){
                    
                    $totalprice=$totalprice+$v["productprice"]*($dishalllist[$cid]-1);
                }
             }else{
                $totalprice=$totalprice+$v["productprice"]*$dishalllist[$cid];
             }
                
         }
                
                
                    
	$data = array(
		'total' =>$totalnum,
        "price"=>$totalprice,
        "goodsinfo"=>serialize($glist)
	);
	pdo_update('shopping3_cart', $data, array('from_user' =>$from , 'weid' => $weid));
                
	$result["totalprice"]=number_format($totalprice,0);
	$result['status'] = 1;
	$result['message'] = '所点菜品数据更新成功！';
	$result['total'] = intval($totalnum);
	message($result, '', 'ajax');
?>